package com.xtck.ha.fault.core.controller;

import com.xtck.ha.common.core.constant.HttpStatus;
import com.xtck.ha.common.core.utils.poi.ExcelUtil;
import com.xtck.ha.common.core.web.controller.BaseController;
import com.xtck.ha.common.core.web.domain.AjaxResult;
import com.xtck.ha.common.core.web.page.TableDataInfo;
import com.xtck.ha.fault.core.model.DsFaultTask;
import com.xtck.ha.fault.core.service.DsFaultTaskService;
import com.xtck.ha.fault.framework.utils.JsonResult;
import com.xtck.ha.fault.modules.pojo.entity.FaultTask;
import com.xtck.ha.fault.modules.pojo.vo.FaultTaskVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 模型列表 controller
 *
 * @ClassName: DsFaultTaskController
 * @Description: 外部访问接口
 * @author liuyuan
 * @date 2025-07-02
 */
@Api(tags = "故障分析任务管理")
@RestController
@RequestMapping("/faultTask")
public class DsFaultTaskController extends BaseController {

    @Autowired
    private DsFaultTaskService dsFaultTaskService;


    @ApiOperation("分页查询故障分析任务列表")
    @GetMapping("/page")
    public AjaxResult page(DsFaultTask dsFaultTask)
    {
        startPage();
        List<DsFaultTask> list = dsFaultTaskService.selectDsFaultTaskList(dsFaultTask);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setList(list);
        rspData.setMsg("查询成功");
        rspData.setTotal(dsFaultTaskService.countDsFaultTask(dsFaultTask));
        return AjaxResult.success(rspData);
    }

    @ApiOperation("列表查询故障分析任务")
    @GetMapping("/list")
    public List<DsFaultTask> list(DsFaultTask dsFaultTask)
    {
        List<DsFaultTask> list = dsFaultTaskService.selectTList(dsFaultTask);
        return list;
    }

    @ApiOperation("导出故障分析任务列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, DsFaultTask dsFaultTask)
    {
        List<DsFaultTask> list = dsFaultTaskService.selectTList(dsFaultTask);
        ExcelUtil<DsFaultTask> util = new ExcelUtil<>(DsFaultTask.class);
        util.exportExcel(response, list, "样例数据");
    }

    @ApiOperation("获取$故障分析任务详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dsFaultTaskService.selectTById(id));
    }

    @ApiOperation("新增$故障分析任务")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody DsFaultTask dsFaultTask)
    {
        return toAjax(dsFaultTaskService.insertT(dsFaultTask));
    }

    @ApiOperation("修改故障分析任务")
    @PutMapping
    public AjaxResult update(@RequestBody DsFaultTask dsFaultTask)
    {
        int i = dsFaultTaskService.updateT(dsFaultTask);
        return toAjax(i);
    }

    @ApiOperation("删除故障分析任务")
	@DeleteMapping
    public AjaxResult remove(@RequestBody List<Long> idList)
    {
        Long[] idArray = idList.toArray(new Long[0]);
        return toAjax(dsFaultTaskService.deleteTByIds(idArray));
    }


    /**
     * 手动试算
     */
    @PostMapping("/computeOnce")
    @ApiOperation(value = "手动试算")
    public AjaxResult computeOnce(@RequestBody DsFaultTask task) {
        task.setIsSchedule(false);
        int result = dsFaultTaskService.save(task);
        if (result == 1){
            task = dsFaultTaskService.selectTList(task).get(0);
            return dsFaultTaskService.computeOnce(task);
        }
        else
            return AjaxResult.error("新增手动任务失败");
    }

    /**
     * 再次试算
     */
    @PostMapping("/computeAgain")
    @ApiOperation(value = "再次试算")
    public AjaxResult computeAgain(@RequestBody DsFaultTask faultTask) {
        faultTask = dsFaultTaskService.selectTList(faultTask).get(0);
        return dsFaultTaskService.computeOnce(faultTask);
    }

    @PostMapping({"/execTask"})
    @ApiOperation(value = "上线运行")
    public AjaxResult exec(@RequestBody DsFaultTask faultTask){
        faultTask.setIsSchedule(true);
        int result = dsFaultTaskService.save(faultTask);
        if (result == 1){
            faultTask = dsFaultTaskService.selectTList(faultTask).get(0);
            return dsFaultTaskService.execTask(faultTask);
        }
        else
            return AjaxResult.error("新增自动任务失败");
    }

    /**
     * 开始任务
     */
    @PostMapping("/start")
    @ApiOperation(value = "开始任务")
    public AjaxResult startTask(@RequestBody DsFaultTask faultTask) {
        faultTask = dsFaultTaskService.selectTById(faultTask.getId());
        return dsFaultTaskService.execTask(faultTask);
    }

    /**
     * 停止任务
     */
    @PostMapping("/stop")
    @ApiOperation(value = "停止任务")
    public AjaxResult stopTask(@RequestBody DsFaultTask faultTask) {
        return dsFaultTaskService.stopTask(faultTask);
    }
}
